package com.zhu.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.zhu.config.JDBCConfig;

/**
 * Druid数据库连接池工具类
 */
public class ZhuDruidDataSourceUtil {

    private static DruidDataSource druidDataSource = null;

    public static DruidDataSource createDataSource(){

        druidDataSource = new DruidDataSource();

        //phoenix Driver
        druidDataSource.setDriverClassName(JDBCConfig.PHOENIX_DRIVER);
        druidDataSource.setUrl(JDBCConfig.PHOENIX_SERVER);
        //初始化连接池数量
        druidDataSource.setInitialSize(5);
        //设置同时活跃最大连接数
        druidDataSource.setMaxActive(20);
        //设置空闲时的最小连接数
        druidDataSource.setMinIdle(1);
        //设置没有空余连接时的等待时间
        druidDataSource.setMaxWait(-1);   //一直等待
        //验证连接是否可使用的sql
        druidDataSource.setValidationQuery("select 1");

        druidDataSource.setTestWhileIdle(true);
        druidDataSource.setTestOnReturn(false);   //
        druidDataSource.setTestOnBorrow(false);

        druidDataSource.setTimeBetweenEvictionRunsMillis(30 * 1000L); // 多少间隔回收一次
        druidDataSource.setMinEvictableIdleTimeMillis(30 * 60 * 1000L);   //定期清理空闲连接 空闲30就要被回收

        return druidDataSource;
    }
}
